package com.likai.jarscms.controller;

import com.likai.jarscms.luence.JarsIndex;
import com.likai.jarscms.pojo.JarBean;
import com.likai.jarscms.service.IJarService;
import com.likai.jarscms.service.ITagService;
import com.likai.jarscms.utils.PageUtil;
import com.likai.jarscms.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * Created by likai on 2018/10/18.
 */
@Controller
@RequestMapping(value = "/jar")
public class JarController {

    /** logger */
    private static final Logger LOGGER = LoggerFactory.getLogger(JarController.class);

    @Autowired
    private ITagService tagService ;

    @Autowired
    private IJarService jarService ;

    private JarsIndex jarsIndex = new JarsIndex() ;

    /**
     * 根据关键字查询jar包相关信息
     * @param q
     * @param mv
     * @return
     */
    @RequestMapping(value = "/q",method = RequestMethod.GET)
    public ModelAndView search(
            String q,
            String page,
            HttpServletRequest request,
            ModelAndView mv
    ) {
        if(StringUtil.isEmpty(page)){
            page="1";
        }
        try {
            List<JarBean> jarList = jarsIndex.searchJar(q.trim(),200) ;
            mv.addObject("q",q) ;
            mv.addObject("resultTotal",jarList.size()) ;
            Integer toIndex=jarList.size()>=Integer.parseInt(page)*20?Integer.parseInt(page)*20:jarList.size();
            mv.addObject("jarList", jarList.subList((Integer.parseInt(page)-1)*20, toIndex));
            mv.addObject("tagList",tagService.randomList(200)) ;
            mv.addObject("pageCode", PageUtil.genPagination(request.getServletContext().getContextPath()+"/jar/q.do", jarList.size(), Integer.parseInt(page), 20, "q="+q));
            mv.setViewName("result");
        } catch (Exception e) {
            LOGGER.error("Exception",e);
        }
        return mv ;
    }

    @RequestMapping(value = "/{id}",method = RequestMethod.GET)
    public ModelAndView minte(
            @PathVariable(value = "id") String id,
            ModelAndView mv
    ){
        try{
            JarBean jar = jarService.findByUuid(id) ;
            //更新访问次数
            jar.setClickhit(jar.getClickhit() + 1);
            jarService.updateJarById(jar) ;
            mv.addObject("jar",jar) ;
            mv.addObject("tagList",tagService.randomList(200)) ;
            mv.addObject("relJarList",jarsIndex.searchJar(jar.getName().replaceAll("-"," "),16)) ;
            mv.setViewName("view");
        }catch (Exception e) {
            LOGGER.error("Exception",e);
        }
        return mv ;
    }

    @RequestMapping(value = "/download/{id}",method = RequestMethod.GET)
    public String download(
            @PathVariable(value = "id") String id,
            HttpServletResponse response
    ){
        try {
            JarBean jar = jarService.findByUuid(id) ;
            //更新下载次数
            jar.setDownhit(jar.getDownhit() + 1);
            jarService.updateJarById(jar) ;
            response.sendRedirect(jar.getPath());
        } catch (IOException e) {
            LOGGER.error("Exception",e);
        }
        return null ;
    }
}
